<!--_meta 作为公共模版分离出去-->
<!DOCTYPE HTML>
<html>
<head>
    <meta charset="utf-8">
    <meta name="renderer" content="webkit|ie-comp|ie-stand">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport"
          content="width=device-width,initial-scale=1,minimum-scale=1.0,maximum-scale=1.0,user-scalable=no"/>
    <meta http-equiv="Cache-Control" content="no-siteapp"/>
    <link rel="Bookmark" href="/favicon.ico">
    <link rel="Shortcut Icon" href="/favicon.ico"/>
    <link rel="stylesheet" type="text/css" href="static/h-ui/css/H-ui.min.css"/>
    <link rel="stylesheet" type="text/css" href="static/h-ui.admin/css/H-ui.admin.css"/>
    <link rel="stylesheet" type="text/css" href="static/h-ui.admin/skin/default/skin.css" id="skin"/>
    <link rel="stylesheet" type="text/css" href="static/h-ui.admin/css/style.css"/>
    <link rel="stylesheet" href="lib/select2/select2.min.css">

    <title>新增任务</title>
</head>
<body>
<article class="page-container">
    <form class="form form-horizontal" id="form-admin-role-add">
        <div class="row cl">
            <label class="form-label col-xs-4 col-sm-3"><span class="c-red">*</span>任务：</label>
            <div class="formControls col-xs-8 col-sm-9">
                <span class="select-box">
                    <select class="select" size="1" name="customId" id="customId">
                        <option value="" selected>--选择任务--</option>
                    </select>
				</span>
            </div>
        </div>
        <div class="row cl">
            <label class="form-label col-xs-4 col-sm-3"><span class="c-red">*</span>任务组：</label>
            <div class="formControls col-xs-8 col-sm-9">
                <input type="text" class="input-text" value="" placeholder="任务组" id="jobGroup" name="jobGroup">
            </div>
        </div>
        <div class="row cl">
            <label class="form-label col-xs-4 col-sm-3"><span class="c-red">*</span>CronExpression：</label>
            <div class="formControls col-xs-8 col-sm-9">
                <input type="text" class="input-text" value="" placeholder="Cron表达式" id="cronExp" name="cronExp">
            </div>
        </div>

        <div class="row cl">
            <div class="col-xs-8 col-sm-9 col-xs-offset-4 col-sm-offset-3">
                <button type="button" id="submit" class="btn btn-success radius" id="admin-role-save"
                        name="admin-role-save"
                ><i class="icon-ok"></i> 提交
                </button>
                <button type="button" class="btn btn-success radius" id="admin-role-close" name="admin-role-close"
                        onclick="layer_close()"><i class="icon-ok"></i> 关闭
                </button>
                <!--<button class="btn" data-dismiss="modal" aria-hidden="true" onclick="layer_close()">关闭</button>-->
            </div>
        </div>
    </form>
</article>

<!--_footer 作为公共模版分离出去-->
<script type="text/javascript" src="static/hry-auto/util.js"></script>
<script type="text/javascript" src="lib/jquery/1.9.1/jquery.min.js"></script>
<script type="text/javascript" src="lib/layer/2.4/layer.js"></script>
<script type="text/javascript" src="static/h-ui/js/H-ui.min.js"></script>
<script type="text/javascript" src="static/h-ui.admin/js/H-ui.admin.js"></script> <!--/_footer 作为公共模版分离出去-->

<!--请在下方写此页面业务相关的脚本-->
<script type="text/javascript" src="lib/jquery.validation/1.14.0/jquery.validate.js"></script>
<script type="text/javascript" src="lib/jquery.validation/1.14.0/validate-methods.js"></script>
<script type="text/javascript" src="lib/jquery.validation/1.14.0/messages_zh.js"></script>
<script type="text/javascript" src="lib/select2/select2.full.js"></script>
<script type="text/javascript" src="lib/select2/pinyin.js"></script>

<script type="text/javascript">
    //loading
    var layerIndex;
    $(function () {
        $.ajaxSetup({
            layerIndex: -1,
            beforeSend: function () { //插件加载前
                layerIndex = layer.load(1, {shade: [1.01, '#fff']});
            },
            error: function () { //报错时执行
                layer.alert('显示异常，请刷新后重试', {
                    icon: 0,
                    skin: 'layer-ext-moon'
                });
            }
        });
    });

    $(document).ready(function () {
        //初始化定制数据源
        custom_selectEle_init("customId");
        $("#jobGroup").val($.cookie("realnameCookie"));
        layer.close(layerIndex);
    });

    //提交表单
    $("#submit").click(function () {
        var customId = $("#customId").val();
        var group = $("#jobGroup").val();
        var cronExp = $("#cronExp").val();
        if (customId.trim == "" || group.trim() == "" || cronExp.trim() == "") {
            layer.msg("任务,任务组,CronExpression必填!");
            return;
        }
        layerIndex = layer.load(1, {shade: [0.01, '#fff']});
        var selectedText = $("#customId").find("option:selected").text();//类似:(127_鲁奇伟_CS1)鲁奇伟的定制测试-Scheduler专用定制

        /*
        var text = selectedText.substring(selectedText.indexOf("(") + 1, selectedText.indexOf(")"))//127_鲁奇伟_CS1
        var strings = text.split("_");
        var customUserName = strings[1];
        var env = strings[2];
        */

        var jobDataMap = {};
        jobDataMap.customId = customId;
        jobDataMap.executeUserId = $.cookie("uidCookie");
        jobDataMap.executeUserName = $.cookie("realnameCookie");
        var param = {};
        param.jobName = customId;
        param.jobGroup = group;
        param.desc = selectedText;
        param.cronExp = cronExp;
        param.jobDataMap = jobDataMap;

        $.ajax({
            type: "post",
            url: "/scheduler/addJob",
            contentType: "application/json;charset=utf-8",
            data: JSON.stringify(param),
            dataType: "json",
            success: function (res) {
                layer.close(layerIndex);
                if (res.status == 0) {//添加成功
                    parent.window.location.reload();
                } else {
                    layer.alert(res.msg, {
                        icon: 0,
                        skin: 'layer-ext-moon'
                    });
                }
            },
            fail: function (res) {
                layer.close(layerIndex);
                layer.alert(JSON.stringify(res), {
                    icon: 0,
                    skin: 'layer-ext-moon'
                });
            },
            error: function (xhr) {
                layer.close(layerIndex);
                layer.alert('Error' + JSON.stringify(xhr), {
                    icon: 2,
                    skin: 'layer-ext-moon'
                });
            }
        });
    });

    //初始化处理
    /*    $(document).ready(function () {
            var skip = getParameter('skip');
            var cId = getParameter('id');
            debugger;
            if(skip == 1){
                var obj = new Object();
                obj.id = cId;
                var json = JSON.stringify(obj);
                $.ajax({
                    type: "post",
                    url: "/tcustom/selectOne",
                    data: json,
                    dataType: "json",
                    contentType: 'application/json;charset=utf-8',
                    success: function (data) {
                        var status = data.status;
                        var msg = data.msg;
                        var serviceList = getServiceList();
                        if (status == 0) {
                            var resData = data.data;
                            $("#customname").val(resData.customname);
                            env_selectEle_init("env");
                            $("#env").val(resData.envid);
                            $("#intersect").val(resData.intersect);
                            $("#strategy").val(resData.strategy);
                            $("#customUser").val(resData.username);
                            debugger;
                            var services = "";
                            for (var i = 0; i < serviceList.length; i++) {
                                var row = serviceList[i];
                                var tClassList = getTClassListByServiceKey(row.servicekey);
                                var tClassHtml = "";
                                for (var m = 0; m < tClassList.length; m++) {
                                    tClassHtml += "<option>" + tClassList[m] + "</option>\n";
                                }
                                var rowHtml = "<dl class=\"permission-list\" id='service-" + row.id + "' style='width: 80%;'>\n" +
                                    "<dt>\n" +
                                    "<input type='text' id='ps_" + row.id + "' value='0'/>" +
                                    "<label style='width:100px;display:inline-block;'>" +
                                    "<input type=\"checkbox\" value=\"" + row.id + "\" name=\"service\" id=\"serviceBox-" + row.id + "\" onclick='clearAll(this," + row.id + ")'/>" + row.servicekey + "</label>\n" +
                                    "<select id='tClass-" + row.id + "'><option>请选择测试类</option>" + tClassHtml + "</select>\n" +
                                    "<label id='show-" + row.id + "'><a title=\"展开\" href=\"javascript:0;\" onclick=\"spread(" + row.id + ")\" class=\"ml-5\" style=\"float:right;text-decoration:none;margin-right:8px;\"><i style='font-size: 18px;' class=\"Hui-iconfont\">&#xe698;</i></a></label>\n" +
                                    "</dt>\n" +
                                    "</dl>";
                                services = services + rowHtml;
                            }
                            $("#services").append(services);

                            var tCustomDetails = resData.tcustomdetails;
                            //先将服务展开,并且选中服务和测试类
                            for (var j = 0; j < tCustomDetails.length; j++) {
                                var tCustomDetail = tCustomDetails[j];
                                if (tCustomDetail.clientlevel == 1) {
                                    var serviceId = tCustomDetail.clientid;
                                    var hasChild = tCustomDetail.haschild;
                                    if (hasChild) {
                                        spread(serviceId);
                                    }
                                    $("#ps_" + serviceId).val(tCustomDetail.priority);
                                    $("#serviceBox-" + serviceId + "").prop("checked", true);
                                    var tClass = tCustomDetail.classname;
                                    $("#tClass-" + serviceId + "").children("option").each(function () {
                                        var temp_value = $(this).val();
                                        if (temp_value == tClass) {
                                            $(this).attr("selected", "selected");
                                        }
                                    });
                                }
                            }

                            //选中接口和用例
                            for (var j = 0; j < tCustomDetails.length; j++) {
                                var tCustomDetail = tCustomDetails[j];
                                var clientId = tCustomDetail.clientid;
                                if (tCustomDetail.clientlevel == 2) {
                                    $("#pi_" + clientId).val(tCustomDetail.priority);
                                    $("#tiBox-" + clientId + "").prop("checked", true);
                                } else if (tCustomDetail.clientlevel == 3) {
                                    $("#pc_" + clientId).val(tCustomDetail.priority);
                                    $("#caseBox-" + clientId + "").prop("checked", true);
                                }
                            }

                            layer.close(layerIndex);

                        } else {
                            layer.close(layerIndex);
                            layer.alert(msg, {
                                icon: 0,
                                skin: 'layer-ext-moon'
                            })
                        }
                    }
                });
            }
            var serviceList = getServiceList();
            env_selectEle_init("env");
            var services = "";
            for (var i = 0; i < serviceList.length; i++) {
                var row = serviceList[i];
                var tClassList = getTClassListByServiceKey(row.servicekey);
                var tClassHtml = "";
                for (var m = 0; m < tClassList.length; m++) {
                    var tClass = tClassList[m];
                    var tClassOption = "<option>" + tClass + "</option>";
                    tClassHtml = tClassHtml + tClassOption;

                }
                var rowHtml = "<dl class=\"permission-list\" id='service-" + row.id + "' style='width: 80%;'>\n" +
                    "<dt>\n" +
                    "<input type='text' id='ps_" + row.id + "' value='0'/>" +
                    "<label style=' width: 100px;display:inline-block;'>" +
                    "<input type=\"checkbox\" value=\"" + row.id + "\" name=\"service\" id=\"serviceBox-" + row.id + "\" onclick='clearAll(this," + row.id + ")'>" + row.servicekey + "</label>\n" +
                    "<select id='tClass-" + row.id + "'><option>请选择测试类</option>" + tClassHtml + "</select>\n" +
                    "<label id='show-" + row.id + "'><a title=\"展开\" href=\"javascript:0;\" onclick=\"spread(" + row.id + ")\" class=\"ml-5\" style=\"float:right;text-decoration:none;margin-right:8px;\"><i style='font-size: 18px;' class=\"Hui-iconfont\">&#xe698;</i></a></label>\n" +
                    "</dt>\n" +
                    "</dl>";
                services = services + rowHtml;

            }
            $("#services").append(services);

            layer.close(layerIndex);

        });*/

    //展开处理
    function spread(serviceId) {
        var list = getAllTiAndCaseByServiceId(serviceId);
        if (list.length == 0) {
            layer.alert("该服务下无接口！");
        } else {
            $("#show-" + serviceId + "").html("");
            var h = "<a title=\"收起\" href=\"javascript:0;\" onclick=\"retract(" + serviceId + ")\" class=\"ml-5\" style=\"float:right;text-decoration:none;margin-right:8px;\"><i style='font-size: 18px;' class=\"Hui-iconfont\">&#xe699;</i></a>";
            $("#show-" + serviceId + "").html(h);

            var tiHtml = "";

            for (var i = 0; i < list.length; i++) {
                var row = list[i];
                var tcases = row.tcases;
                var caseHtml = "";
                for (var k = 0; k < tcases.length; k++) {
                    var tcase = tcases[k];
                    var tcastreamark = tcase.remark;
                    if (tcastreamark == null) {
                        tcastreamark = "";
                    }
                    caseHtml += "<dd>\n" +
                        "<dl id='case-" + tcase.id + "' style=''\n" +
                        "<dt>\n" +
                        "<input type='text' id='pc_" + tcase.id + "' value='0' style='height:16px'/>" +
                        "<label style='font-size:12px;' title='" + tcastreamark + "'>" +
                        "<input style='width:16px;height: 16px;' type=\"checkbox\" value=\"" + tcase.id + "\" name=\"tcase-" + serviceId + "-" + row.id + "\" id=\"caseBox-" + tcase.id + "\" onclick='caseChecked(this," + row.id + "," + serviceId + ")'>"
                        + tcase.casename + "(" + "<a title=\"用例编辑\" href=\"javascript:void(0);\" onclick=\"tcase_edit('用例编辑','tcase-edit.html','" + tcase.id + "','1')\" style=\"text-decoration: underline;color:#0000ff;\" >" + tcase.id + "</a>" + "-" + tcase.author + ")</label>\n" +
                        "</dt>\n" +
                        "</dl>\n" +
                        "</dd>";
                }
                tiHtml += "<dd>\n" +
                    "<dl id='ti-" + row.id + "'>\n" +
                    "<dt style='float:none;'>\n" +
                    "<input type='text' id='pi_" + row.id + "' value='0' style='height:19px'/>" +
                    "<label style='font-size:14px;'>" +
                    "<input style='width:19px;height: 19px;' type=\"checkbox\" value=\"" + row.id + "\" name=\"ti-" + serviceId + "\" id=\"tiBox-" + row.id + "\" onclick='tiChecked(this," + serviceId + "," + row.id + ")'>" + row.iuri + "(" + row.remark + ")" +
                    "</label>\n" +
                    "</dt>\n" +
                    "" + caseHtml + "\n" +
                    "</dl>\n" +
                    "</dd>\n";
            }
        }
        $("#service-" + serviceId + "").append(tiHtml);
    }

    //收起处理
    function retract(serviceId) {
        $("#service-" + serviceId + "").children('dd').hide();
        $("#show-" + serviceId + "").html("");
        var h = "<a title=\"展开\" href=\"javascript:0;\" onclick=\"showTi(" + serviceId + ")\" class=\"ml-5\" style=\"float:right;text-decoration:none;margin-right:8px;\"><i style='font-size: 18px;' class=\"Hui-iconfont\">&#xe698;</i></a>";
        $("#show-" + serviceId + "").html(h);

    }

    //显示服务下所有接口用例
    function showTi(serviceId) {
        $("#service-" + serviceId + "").children('dd').show();
        $("#show-" + serviceId + "").html("");
        var h = "<a title=\"收起\" href=\"javascript:0;\" onclick=\"retract(" + serviceId + ")\" class=\"ml-5\" style=\"float:right;text-decoration:none;margin-right:8px;\"><i style='font-size: 18px;' class=\"Hui-iconfont\">&#xe699;</i></a>";
        $("#show-" + serviceId + "").html(h);
    }

    function tiChecked(obj, serviceId, tiId) {
        var tcases = document.getElementsByName("tcase-" + serviceId + "-" + tiId + "");
        var tis = document.getElementsByName("ti-" + serviceId + "");
        var s = document.getElementById("serviceBox-" + serviceId + "");
        for (var i = 0; i < tcases.length; i++) {
            if (obj.checked == true) {
                //tcases[i].checked = true;
                s.checked = true;
            } else {
                tcases[i].checked = false;
            }

        }
        var b = true;
        for (var j = 0; j < tis.length; j++) {
            if (tis[j].checked == true) {
                s.checked = true;
                b = false;
            }
        }

        if (b == true) {
            s.checked = false;
        }


    }

    function caseChecked(obj, tiId, serviceId) {
        var tcase = document.getElementsByName("tcase-" + serviceId + "-" + tiId + "");
        var tis = document.getElementsByName("ti-" + serviceId + "");
        var s = document.getElementById("serviceBox-" + serviceId + "");
        if (obj.checked == true) {
            document.getElementById("tiBox-" + tiId + "").checked = true;
            s.checked = true;

        } else {
            var b = true;
            for (var i = 0; i < tcase.length; i++) {
                if (tcase[i].checked == true) {
                    b = false;
                }
            }

            if (b) {
                document.getElementById("tiBox-" + tiId + "").checked = false;
                var c = true;
                for (var j = 0; j < tis.length; j++) {
                    if (tis[j].checked == true) {
                        c = false;
                    }
                }
                if (c) {
                    s.checked = false;
                }

            }


        }

    }

    /*用例-编辑*/
    function tcase_edit(title, url, id, skip) {
        url = url + "?id=" + id + "&skip=" + skip;
        layer_show(title, url);
    }

    function clearAll(obj, serviceId) {
        if (obj.checked == false) {
            $("#service-" + serviceId + " :checkbox").prop("checked", false);
        }
    }

    //添加定制测试
    function addCustom() {
        var customname = $("#customname").val();
        var env = $("#env").val();
        var envKey = $("#env").find("option:selected").text();
        var strategy = $("#strategy").val();
        var userid = $.cookie('uidCookie');
        var userName = $.cookie("realnameCookie");
        var intersect = $("#intersect").val();

        var service = new Array();
        $("#services :checkbox:checked").each(function (i) {
            var value = $(this).val();
            var name = $(this).attr("name");
            if (name.match("service")) {
                var tClass = $("#tClass-" + value + "").val();
                if (tClass == "请选择测试类") {
                    tClass = "";
                }
                var s = new Object();
                s.clientlevel = 1;
                s.clientid = value;
                /*s.clientname=getServiceKey(value);*/
                var ps = $("#ps_" + value).val();
                s.priority = ps;
                var innerText = document.getElementById("serviceBox-" + value).parentNode.innerText;
                s.clientname = innerText;
                s.classname = tClass;
                s.parentclientid = 0;
                var obj = $("#service-" + value + " :checkbox:checked");
                if (obj.length > 1) {
                    s.haschild = true;
                } else {
                    s.haschild = false;
                }
                service[i] = s;

            } else if (name.match("ti")) {
                //var ti=getTiById(value);
                var s = new Object();
                s.clientlevel = 2;
                s.clientid = value;
                // s.clientname=ti.iuri;
                // s.parentclientid=ti.serviceid;
                var pi = $("#pi_" + value).val();
                s.priority = pi;
                var tiLabel = document.getElementById("tiBox-" + value);
                var tiInnerText = tiLabel.parentNode.innerText;
                var clientName = tiInnerText.substring(0, tiInnerText.indexOf("("));
                s.clientname = clientName;

                var tiDl = document.getElementById("ti-" + value);
                var tserviceIDAttr = tiDl.parentNode.parentNode.getAttribute("id");
                var sid = tserviceIDAttr.substring(tserviceIDAttr.indexOf("-") + 1);
                s.parentclientid = sid;

                var obj = $("#ti-" + value + " :checkbox:checked");
                if (obj.length > 1) {
                    s.haschild = true;
                } else {
                    s.haschild = false;
                }
                service[i] = s;

            } else if (name.match("tcase")) {
                // var tcase=getTcaseById(value);
                var s = new Object();
                s.clientlevel = 3;
                s.clientid = value;
                // s.clientname=tcase.casename;
                // s.parentclientid=tcase.iid;
                var pc = $("#pc_" + value).val();
                s.priority = pc;
                var tcaseLabel = document.getElementById("caseBox-" + value);
                var tcaseInnerText = tcaseLabel.parentNode.innerText;
                var tcaseclientname = tcaseInnerText.substring(0, tcaseInnerText.indexOf("("));
                s.clientname = tcaseclientname;
                var tcaseDl = document.getElementById("case-" + value);
                var tiIDAttr = tcaseDl.parentNode.parentNode.getAttribute("id");
                var iid = tiIDAttr.substring(tiIDAttr.indexOf("-") + 1);
                s.parentclientid = iid;
                s.haschild = false;
                service[i] = s;
            }
        });

        if (customname == null || customname == "") {
            layer.msg("定制名称不能为空！");
            return;
        } else if (service == null || service == "") {
            layer.msg("请选择服务！");
            return;
        } else if (env == null || env == "") {
            layer.msg("请选择环境！");
            return;
        } else {
            layerIndex = layer.load(1, {shade: [0.01, '#fff']});
            var obj = new Object();
            obj.customname = customname;
            obj.userid = userid;
            obj.username = userName;
            obj.envid = env;
            obj.envkey = envKey;
            obj.strategy = strategy;
            obj.intersect = intersect;
            obj.tcustomdetails = service;
            var json = JSON.stringify(obj);
            console.log(json);

            $.ajax({
                type: "post",
                url: "/tcustom/insertOne",
                data: json,
                dataType: "json",
                contentType: 'application/json;charset=utf-8',
                success: function (data) {
                    var status = data.status;
                    var msg = data.msg;
                    if (status == 0) {
                        layer.close(layerIndex);
                        parent.window.location.reload();
                        var index = parent.layer.getFrameIndex(window.name);
                        parent.layer.close(index);
                    } else {
                        layer.close(layerIndex);
                        layer.alert(msg, {
                            icon: 0,
                            skin: 'layer-ext-moon'
                        })
                    }

                },
                fail: function (data) {
                    layer.close(layerIndex);
                    layer.alert(JSON.stringify(data), {
                        icon: 0,
                        skin: 'layer-ext-moon'
                    })
                },
                error: function (xhr) {
                    layer.close(layerIndex);
                    layer.alert('Error' + JSON.stringify(xhr), {
                        icon: 2,
                        skin: 'layer-ext-moon'
                    })
                }
            });
        }

    }


</script>

</body>
</html>